package DatabasePackage;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;


/**
 * This class provides the means for accessing the different tables in the
 * database. To accessing a certain table, one needs to create the appropriate
 * "connector" class, i.e. if we want to access the User's table, we create the
 * UserDBTable class. The complete documentation and listing of the databases
 * used, can be found in the projects' Low level design documentation. Below
 * their is a complete list of methods for creating these "connector" classes.
 * 
 * @author Evyatar Lavian, Tamir Carmeli
 */
public class DatabaseManager {
  public enum DBTables {
    USER, COURSE, SESSION,USER_COURSE;
  }
  
  static private Map<String, DBtable> dbs = new HashMap<String, DBtable>();
  static String driver;
  static String url; 
  static String user; 
  static String password;
  public DatabaseManager(String driver,String url, String user, String password ,Map<String, DBtable> dbs) {
	DatabaseManager.dbs = dbs;
    DatabaseManager.driver = driver;
    DatabaseManager.url = url;
    DatabaseManager.user = user;
    DatabaseManager.password = password;
  }
  public DatabaseManager() {
	  }
  
  public DatabaseManager(String driver,String url, String user, String password) {
	DatabaseManager.driver = driver;
	DatabaseManager.url = url;
	DatabaseManager.user = user;
	DatabaseManager.password = password;
    for (DBTables db : DBTables.values()) {
      String dbName = db.toString();
      switch (db) {
        case USER:
          dbs.put(dbName, new UserDBTable());
          break;
        case COURSE:
          dbs.put(dbName, new CourseDBTable());
          break;
        case SESSION:
          dbs.put(dbName, new SessionDBTable());
          break;
        case USER_COURSE:
          dbs.put(dbName, new UserCoursesDBTable());
          break;
      }
    }
  }
  
  /**
   * This method creates the various datatables in our system. In case the
   * tables already exist no operation will be performed.
   * 
   * @throws SQLException
   *           in case our system is unable to connect to the SQL server or in
   *           case a databse error has occurred
   * @throws ClassNotFoundException
   *           in case java can't find the appropriate connector to MySQL - in
   *           our case Connector/J
   */
  public void initializeDBs() throws ClassNotFoundException, SQLException {
    for (DBtable db : dbs.values()) {
      db.initializeDB(driver, url, user, password);
    }
  }
  
  /**
   * This method cleans up the various datatables in our system.
   * 
   * @throws SQLException
   *           in case our system is unable to connect to the SQL server or in
   *           case a databse error has occurred
   * @throws ClassNotFoundException
   *           in case java can't find the appropriate connector to MySQL - in
   *           our case Connector/J
   */
  public void cleanupDBs() throws ClassNotFoundException, SQLException {
    for (DBtable db : dbs.values())
      db.cleanupDB();
  }
  
  /**
   * This method retrieves a specific DBtable class accroding to the table
   * (enum) specified.
   * 
   * @param table
   *          the requested table
   * @return
   */
  public DBtable getTable(DBTables table) {
    return dbs.get(table.toString());
  }
}
